package org.utils.rmi;

import java.io.IOException;
import java.lang.reflect.Constructor;
import java.net.ServerSocket;
import java.rmi.server.RMIServerSocketFactory;

public class SSLServerSocketFactory implements RMIServerSocketFactory
{
	private static final String DEFAULT_CLASS_NAME = "javax.net.ssl.SSLServerSocket";
	
	private Constructor cons = null;
	
	public static SSLServerSocketFactory getDefaultFactory()
		throws SSLSocketFactoryException
	{
		return SSLServerSocketFactory.getFactory(DEFAULT_CLASS_NAME);
	}
	
	public static SSLServerSocketFactory getFactory(String className)
		throws SSLSocketFactoryException
	{
		return new SSLServerSocketFactory(className);
	}

	public static SSLServerSocketFactory getFactory(Class pClass)
		throws SSLSocketFactoryException
	{
		return new SSLServerSocketFactory(pClass);
	}

	protected SSLServerSocketFactory(String sslSocketClassName)
		throws SSLSocketFactoryException
	{
	   try
	   {
		Class sslSocketClass = Class.forName(sslSocketClassName);
		Class[] cTypes = { Integer.TYPE };
		cons = sslSocketClass.getConstructor(cTypes);
	   }
	   catch (Exception e)
	   {
	   	throw new SSLSocketFactoryException(e);
	   }
	}

	protected SSLServerSocketFactory(Class sslSocketClass)
		throws SSLSocketFactoryException
	{
	   try
	   {
		Class[] cTypes = { Integer.TYPE };
		cons = sslSocketClass.getConstructor(cTypes);
	   }
	   catch (Exception e)
	   {
	   	throw new SSLSocketFactoryException(e);
	   }
	}
	
	public ServerSocket createServerSocket(int port) 
		throws IOException
 	{
	   try
	   {
		Object[] args = { new Integer(port) };
		return (ServerSocket) cons.newInstance(args);
	   }
	   catch (Exception e)
	   {
	   	throw new IOException(e.toString());
	   }
 	}
}
